package org.springblade.common.config;

import org.quartz.Trigger;
import org.springblade.modules.monitor.entity.Application;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * @ClassName QuartzConfiguration
 * @Description 定时任务，轮询数据库
 * @Author zhang_xfei
 * @DATE 2020 2020/5/18 14:09
 **/
@Configuration
public class QuartzConfiguration {

	@Autowired
	@Qualifier("jobDetail")
	private MethodInvokingJobDetailFactoryBean jobDetail;
	@Autowired
	@Qualifier("applicationDetail")
	private MethodInvokingJobDetailFactoryBean applicationDetail;
	@Autowired
	@Qualifier("chainRelationshipDetail")
	private MethodInvokingJobDetailFactoryBean chainRelationshipDetail;

	@Bean(name = "jobDetail")
	public MethodInvokingJobDetailFactoryBean jobDetail(ScheduleTask task) {// ScheduleTask为需要执行的任务
		MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
		/*
		 *  是否并发执行
		 *  例如每5s执行一次任务，但是当前任务还没有执行完，就已经过了5s了，
		 *  如果此处为true，则下一个任务会执行，如果此处为false，则下一个任务会等待上一个任务执行完后，再开始执行
		 */
		jobDetail.setConcurrent(false);
		// 设置任务的名字
		jobDetail.setName("work-order-alert");
		// 设置任务的分组，这些属性都可以存储在数据库中，在多任务的时候使用
		jobDetail.setGroup("work-order");
		/*
		 * 为需要执行的实体类对应的对象
		 */
		jobDetail.setTargetObject(task);
		/*
		 * 通过这几个配置，告诉JobDetailFactoryBean我们需要执行定时执行ScheduleTask类中的sayHello方法
		 */
		jobDetail.setTargetMethod("alert");
		return jobDetail;
	}

	@Bean(name = "applicationDetail")
	public MethodInvokingJobDetailFactoryBean applicationDetail(ApplicationTask task) {// ScheduleTask为需要执行的任务
		MethodInvokingJobDetailFactoryBean applicationDetail = new MethodInvokingJobDetailFactoryBean();
		applicationDetail.setConcurrent(false);
		applicationDetail.setName("blade_t_application_job");
		applicationDetail.setGroup("blade_t_application_group");
		applicationDetail.setTargetObject(task);
		applicationDetail.setTargetMethod("applicationSocketScan");
		return applicationDetail;
	}


	@Bean(name = "chainRelationshipDetail")
	public MethodInvokingJobDetailFactoryBean chainRelationshipDetail(ChainRelationshipTask task) {// ScheduleTask为需要执行的任务
		MethodInvokingJobDetailFactoryBean applicationDetail = new MethodInvokingJobDetailFactoryBean();
		applicationDetail.setConcurrent(false);
		applicationDetail.setName("blade_t_relationship_job");
		applicationDetail.setGroup("blade_t_relationship_group");
		applicationDetail.setTargetObject(task);
		applicationDetail.setTargetMethod("chainRelationshipScan");
		return applicationDetail;
	}


	@Bean(name = "cronJobTrigger")
	public CronTriggerFactoryBean cronJobTrigger() {
		CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
		tigger.setJobDetail(jobDetail.getObject());
		// 初始时的cron表达式  每20s 执行一次
		tigger.setCronExpression("0/20 * * * * ?");
		// trigger的name
		tigger.setName("work-order-trigger");
		return tigger;

	}

	@Bean(name = "cronApplicationTrigger")
	public CronTriggerFactoryBean cronApplicationTrigger() {
		CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
		tigger.setJobDetail(applicationDetail.getObject());
		tigger.setCronExpression("0/20 * * * * ?");
		tigger.setName("blade_t_application_trigger");
		return tigger;
	}

	@Bean(name = "cronRelationshipTrigger")
	public CronTriggerFactoryBean cronRelationshipTrigger() {
		CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
		tigger.setJobDetail(chainRelationshipDetail.getObject());
		tigger.setCronExpression("0/20 * * * * ?");
		tigger.setName("blade_t_relationship_trigger");
		return tigger;
	}

	@Bean(name = "scheduler")
	public SchedulerFactoryBean schedulerFactory(Trigger cronJobTrigger,Trigger cronApplicationTrigger,Trigger cronRelationshipTrigger) {
		SchedulerFactoryBean bean = new SchedulerFactoryBean();
		// 用于quartz集群,QuartzScheduler 启动时更新己存在的Job
		bean.setOverwriteExistingJobs(true);
		// 延时启动，应用启动1秒后
		bean.setStartupDelay(1);
		// 注册触发器
		bean.setTriggers(cronJobTrigger,cronApplicationTrigger,cronRelationshipTrigger);
		return bean;
	}
}
